package com.chat.view;

import com.chat.service.UserClientService;
import com.chat.utils.MouseFollow;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import static javafx.scene.input.KeyCode.*;

public class LoginController implements Initializable {

     @FXML
    private Button cancelbutton;
   @FXML
   private Label loginMessageLabel;
   @FXML
   private ImageView brandingImageView;
   @FXML
   private ImageView usernameImageView;
   @FXML
   private ImageView passwordImageView;
   @FXML
   private TextField usernameTextField;
   @FXML
   private PasswordField enterPasswordField;
   @FXML
   private Button registerbutton;
   @FXML
   private Button forgetbutton;//忘记密码按钮
    @Override
       public void initialize(URL url, ResourceBundle resourceBundle){
        File brandingFile=new File("D:\\Code\\java\\project\\src\\main\\resources\\Login\\2.png");
        Image brandingImage=new Image(brandingFile.toURI().toString());
        brandingImageView.setImage(brandingImage);
        File usernameFile=new File("D:\\Code\\java\\project\\src\\main\\resources\\Login\\用户_fill.png");
        Image usernameImage=new Image(usernameFile.toURI().toString());
        usernameImageView.setImage(usernameImage);
        File passwordFile=new File("D:\\Code\\java\\project\\src\\main\\resources\\Login\\密码.png");
        Image passwordImage=new Image(passwordFile.toURI().toString());
        passwordImageView.setImage(passwordImage);
    }

       public void loginclick(){//登录按钮的点击事件

         if(usernameTextField.getText().isEmpty()==false&&enterPasswordField.getText().isEmpty()==false){
             validateLogin();
         }else{
             loginMessageLabel.setText("用户名和密码不能为空！");
         }
   }

       public void cancelbuttonOnAction(ActionEvent event){//关闭窗口
         Stage stage=(Stage) cancelbutton.getScene().getWindow();
         stage.close();
     }

       public void validateLogin(){
            int rs=UserClientService.checkUser(usernameTextField.getText().toString(),enterPasswordField.getText().toString());
               if(rs==1)
               {    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("提示");
                    alert.setHeaderText("恭喜");
                    alert.setContentText("登录成功！");
                    alert.showAndWait();
                     loginMessageLabel.setText("");
                     Stage stage=(Stage) cancelbutton.getScene().getWindow();
                     stage.close();
                      createmainwindow();
               }
               else if(rs==2){
                        loginMessageLabel.setText("该账号已登录！");
               }
               else if(rs==0){
                     loginMessageLabel.setText("用户名或密码错误！请再尝试一次！");
               }
     }

       public void onkeypressed(KeyEvent keyEvent){

        if(keyEvent.getCode()== KeyCode.ENTER){
            loginclick();
        }
    }

       public void registerbuttonOnAction(ActionEvent event){
         Stage stage=(Stage) cancelbutton.getScene().getWindow();
         stage.close();
         createRegisterForm();
     }

       public void createRegisterForm(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("register.fxml"));
            Parent root = fxmlLoader.load();
            Stage regitsterstage=new Stage();
            Scene scene = new Scene(root, 600, 730);
            regitsterstage.initStyle(StageStyle.TRANSPARENT);
            MouseFollow in = new MouseFollow(root, regitsterstage);
            regitsterstage.setScene(scene);
            regitsterstage.show();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

       public void createmainwindow(){
         try {
             MainWindowView.start(new Stage());
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

       public void forgetbuttonOnAction(ActionEvent event){
          Stage stage=(Stage) cancelbutton.getScene().getWindow();
          stage.close();
          createforgetForm();
       }

       public void createforgetForm(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("forget1.fxml"));
            Parent root = fxmlLoader.load();
            Stage forgetstage=new Stage();
            Scene scene = new Scene(root, 550, 400);
            forgetstage.initStyle(StageStyle.TRANSPARENT);
            MouseFollow in = new MouseFollow(root, forgetstage);
            forgetstage.setScene(scene);
            forgetstage.show();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

       public void emaillogin(){
           Stage stage=(Stage) cancelbutton.getScene().getWindow();
                stage.close();
           try {
               EmailLoginView.start(new Stage());
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

}
